import Message from "element-ui/packages/message/src/main";
import * as fileAPI from "../../api/fileAPI";
import {Loading} from "element-ui";

const fileStorage = {
    state: {},
    mutations: {},
    actions: {
        getFile: async ({commit}, fileName) => {
            let res = fileAPI.getFileAPI(fileName);
            if (!res) {
                Message({
                    message: "文件获取失败失败",
                    type: "error"
                });
            } else {
                //TODO downFile
            }
        },
        getFileList: async ({commit}) => {
            let res = fileAPI.getFileListAPI();
            if (!res) {
                Message({
                    message: "文件列表获取失败",
                    type: "error"
                });
            }
        },
        deleteFile: async ({commit}, params) => {
            let res = fileAPI.deleteFileAPI({nodeId: params.nodeId, fileName: params.fileName});
            if (!res) {
                console.log("图片删除失败");
            }
        },
        uploadAvatar: async ({commit}, params) => {
            let res = fileAPI.uploadAvatarAPI(params);
            if (!res) {
                Message({
                    message: "头像上传失败",
                    type: "error"
                });
            } else {
                let f = res.then((result) => {
                    console.log(result);
                    let avatarItem = document.getElementById("avatar");
                    avatarItem.setAttribute("src", result.name);
                });
                Message({
                    message: "头像上传成功",
                    type: "success"
                });
            }
            return res;
        },
        uploadGraphCover: async ({commit}, params) => {
            let res = fileAPI.uploadGraphCoverAPI(params);
            if (!res) {
                Message({
                    message: "封面上传失败",
                    type: "error"
                });
            } else {
                Message({
                    message: "封面上传成功",
                    type: "success"
                });
            }
        },
        uploadNodeImage: async ({commit}, params) => {
            let res = fileAPI.uploadPictureAPI(params);
            if (!res) {
                Message({
                    message: "图片上传失败",
                    type: "error"
                });
            } else {
                Message({
                    message: "图片上传成功",
                    type: "success"
                });
            }
        },
    }
};

export default fileStorage;